package com.life.service.impl;

import com.life.constant.MessageConstant;
import com.life.constant.StatusConstant;
import com.life.exception.ArticleNotFoundException;
import com.life.exception.ArticleNotPubliclyException;
import com.life.mapper.ArticleMapper;
import com.life.pojo.entity.Article;
import com.life.pojo.vo.ArticleVO;
import com.life.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    /**
     * 根据id查询文章
     * @param id
     * @return
     */
    @Override
    public ArticleVO getArticleById(Long id) {
        Article article = articleMapper.selectById(id);
        if(article == null){
            throw new ArticleNotFoundException(MessageConstant.ARTICLE_NOT_FOUND);
        }
        if(article.getIsDeleted().equals(StatusConstant.ENABLE)){
            throw new ArticleNotFoundException(MessageConstant.ARTICLE_NOT_FOUND);
        }
        if(article.getStatus().equals(StatusConstant.DISABLE)){
            throw new ArticleNotPubliclyException(MessageConstant.ARTICLE_NOT_PUBLICLY);
        }
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(article, articleVO);
        return articleVO;
    }
}
